import {axiosClient} from "@/utils/axiosClient.js";
import {handleCommonParams} from "@/api/commonParams.js";

const requestUrl = '/template/processNode'

const api = {

    /**
     * 新增模板节点定义
     * @param processNode
     * @returns {Promise<*|undefined>}
     */
    createProcessNode: (processNode) =>
      axiosClient().post(`${requestUrl}/addInfo`, processNode),

    /**
     * 根据ID删除模板节点定义
     * @param id
     * @returns {Promise<*|undefined>}
     */
    deleteProcessNodeById: (id) =>
        axiosClient().remove(`${requestUrl}/removeInfo/${id}`),

    /**
     * 根据ID全量更新模板节点定义
     * @param id
     * @param processNode
     * @returns {Promise<*|undefined>}
     */
    updateProcessNodeFull: (id, processNode) =>
        axiosClient().put(`${requestUrl}/modifyInfo/${id}`, processNode),

    /**
     * 根据ID部分更新模板节点定义
     * @param id
     * @param updatePartial
     * @returns {Promise<*|undefined>}
     */
    updateProcessNodePartial: (id, updatePartial) =>
        axiosClient().patch(`${requestUrl}/modifyPartialInfo/${id}`, updatePartial),

    /**
     * 根据ID查询模板节点定义
     * @param id
     * @returns {Promise<*|undefined>}
     */
    getProcessNodeById: (id) =>
        axiosClient().get(`${requestUrl}/getInfo/${id}`),

    /**
     * 分页查询模板节点定义
     * @param page
     * @param size
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getProcessNodePage: (page, size, businessParam) => {
        const params = new URLSearchParams();
        params.append('page', page)
        params.append('size', size)
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfos?${params.toString()}`)
    },

    /**
     * 根据条件查询模板节点定义
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getProcessNodeList: (businessParam) => {
        const params = new URLSearchParams();
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfoList?${params.toString()}`)
    }

}

const setBusinessParams = (params, businessParam) => {
    if (businessParam) {
        if (businessParam.processId) {
            params.append('processId', businessParam.processId)
        }
        if (businessParam.nodeCode) {
            params.append('nodeCode', businessParam.nodeCode)
        }
        if (businessParam.nodeName) {
            params.append('nodeName', businessParam.nodeName)
        }
        if (businessParam.nodeType) {
            params.append('nodeType', businessParam.nodeType)
        }
        if (businessParam.draftFlag) {
            params.append('draftFlag', businessParam.draftFlag)
        }
        if (businessParam.participantRule) {
            params.append('participantRule', businessParam.participantRule)
        }
        if (businessParam.formId) {
            params.append('formId', businessParam.formId)
        }
        if (businessParam.nodeLoadFunc) {
            params.append('nodeLoadFunc', businessParam.nodeLoadFunc)
        }
        if (businessParam.clickNodeFunc) {
            params.append('clickNodeFunc', businessParam.clickNodeFunc)
        }
        if (businessParam.extendSign) {
            params.append('extendSign', businessParam.extendSign)
        }
        handleCommonParams(params, businessParam);
    }
}

export default api
